package com.itqf.mybatis;

import com.itqf.bean.Menu;
import com.itqf.mapper.MenuMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * projectName: mybatis2103
 *
 * @author: 赵伟风
 * time: 2021/6/24 10:14
 * description:
 */
public class UseMybatisSelect {

    public static void main(String[] args) throws Exception{


        //1.读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2.创建sqlsesssionfactorybuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.创建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4.创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //5.获取mapper
        MenuMapper mapper = sqlSession.getMapper(MenuMapper.class);
        //6.调用mapper的方法
        List<Menu> menus = mapper.queryMenusBySort(2, 100);

        System.out.println("menus = " + menus);

        //7.关闭资源
        sqlSession.close();

    }
}
